// pages/addressSet/addressSet.js
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList: []
  },
  changeAdd(e){
    let str = JSON.stringify(e.currentTarget.dataset.name)
    wx.navigateTo({
      url: '../addressChange/addressChange?jsonStr='+str
    })
  },
  onClose(event) {
    const that = this
    const { position, instance } = event.detail;
    switch (position) {
      case 'cell':
        instance.close();
        break;
      case 'right':
        instance.close();
        Dialog.confirm({
          message: '确定删除吗？'
        }).then(() => {
          wx.request({
            url: app.globalData.baseUrl + '/shipping_address/delShippingAddressByUser',
            data: {
              xcxOpenId: app.globalData.xcxOpenId,
              id: event.currentTarget.dataset.name.id
            },
            method:'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success:function(res){
              if (res.data.code == 0) {
                that.searchList()
              }
            }
          })
        });
        break;
    }
  },
  searchList(){
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressListByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      method:'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        that.setData({
          addList: res.data.data
        })
      }
    })
  },
  newaddressto(){
    wx.navigateTo({
      url: '../newAddress/newAddress'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.searchList()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.reLaunch({
      url: '../me/me',
    })
  }
})